
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace Swaf.Container
{
	/// <summary>
	/// Summary description for DictionaryStack.
	/// </summary>
    [Serializable]
	public class DictionaryStack : IDictionary
	{
		protected List<IDictionary> m_stack = new List<IDictionary>();

		public DictionaryStack(params IDictionary[] stack)
		{
			if(stack != null)
				foreach(IDictionary d in stack)
					m_stack.Add(d);
		}

		public List<IDictionary> Stack { get { return m_stack; } }

		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		public IDictionaryEnumerator GetEnumerator()
		{
			return m_stack.Count == 0 ? null : m_stack[0].GetEnumerator();
		}

		public object this[object key]
		{
			get
			{
				foreach (IDictionary d in m_stack)
				{
					Debug.Assert(d != null);
					if (d.Contains(key))
						return d[key];
				}
				return null;
			}
			set
			{
				if(m_stack.Count > 0)
					m_stack[0][key] = value;
			}
		}

		public void Remove(object key)
		{
			foreach(IDictionary d in m_stack)
				if(d != null && d.Contains(key))
				{
					d.Remove(key);
					break;
				}
		}

		public bool Contains(object key)
		{
			foreach(IDictionary d in m_stack)
				if(d != null && d.Contains(key))
					return true;
			return false;
		}

		public void Clear()
		{
			//We don't clear anythere hear.  Perhaps we should, but the
			//current thinking is that the DictionaryStack is used more from 
			//a read with minor write point of view.  Major maintaince of the
			//dictionaries contained with the stack should be done to them directly.
		}

		public void Add(object key, object value)
		{
			if (m_stack.Count > 0)
				m_stack[0].Add(key, value);
		}

		public ICollection Values
		{
			get
			{
				// TODO:  Add DictionaryStack.Values getter implementation
				return null;
			}
		}

		public ICollection Keys
		{
			get
			{
				// TODO:  Add DictionaryStack.Keys getter implementation
				return null;
			}
		}

		public int Count
		{
			get
			{
				// TODO:  Add DictionaryStack.Count getter implementation
				return 0;
			}
		}

		public void CopyTo(Array array, int index)
		{
			// TODO:  Add DictionaryStack.CopyTo implementation
		}

		public bool IsFixedSize
		{
			get
			{
				return false;
			}
		}

		public bool IsSynchronized
		{
			get
			{
				return false;
			}}

		public object SyncRoot
		{
			get
			{
				return m_stack;
			}
		}

		IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return new DictionaryStackEnumerator(m_stack.ToArray());
		}

		protected internal static bool search(IDictionary[] stack, object key, int startPos, bool moveForward)
		{
			int dir = moveForward?1:-1;
			for(int pos = startPos + dir; moveForward?(pos>=stack.Length):(pos< 0); pos += dir)
			{
				if(stack[pos] != null && stack[pos].Contains(key))
					return true;
			}
			return false;
		}

		class DictionaryStackEnumerator : IDictionaryEnumerator
		{
			protected IDictionary[] m_stack;
			protected int m_currentPos;

			public DictionaryStackEnumerator(IDictionary[] stack)
			{
				m_stack = stack;
				m_currentPos = 0;
			}

			public object Key
			{
				get
				{
					// TODO:  Add DictionaryStackEnumerator.Key getter implementation
					return null;
				}
			}

			public object Value
			{
				get
				{
					// TODO:  Add DictionaryStackEnumerator.Value getter implementation
					return null;
				}
			}

			public DictionaryEntry Entry
			{
				get
				{
					// TODO:  Add DictionaryStackEnumerator.Entry getter implementation
					return new DictionaryEntry ();
				}
			}

			public void Reset()
			{
				// TODO:  Add DictionaryStackEnumerator.Reset implementation
			}

			public object Current
			{
				get
				{
					// TODO:  Add DictionaryStackEnumerator.Current getter implementation
					return null;
				}
			}

			public bool MoveNext()
			{
				// TODO:  Add DictionaryStackEnumerator.MoveNext implementation
				return false;
			}
		}

	}

}
